package com.jp.knowledge.activity;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.fragment.JoinOrgnazitionFragment;
import com.jp.knowledge.model.AuthInfo;
import com.jp.knowledge.my.model.OrganizaModel;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jiayen     2017-03-23 15:45:39
 *
 * 认领组织接口
 */
public class JoinOrgnazitionAcitivity extends CompanyBaseAcytivity {


    private OrganizaModel company;

    @Override
    protected String getTitleString() {
        return "认领组织";
    }

    @Override
    protected Fragment getFragment(OrganizaModel company) {
        return new JoinOrgnazitionFragment().newInstance(company);
    }


    /**
     * 重写获取公司信息方法
     * @return
     */
    @Override
    protected List<OrganizaModel> getCompanys() {// 0未认证，1已认证，2认证中 3，不通过

        List<OrganizaModel> companys = new ArrayList<>();
        try {
            company = (OrganizaModel) getIntent().getSerializableExtra("company");
        }catch (Exception e){

        }
        if(company==null) {
            List<OrganizaModel> list = userData.getCompanyList();
            for (OrganizaModel mode : list) {
                int state = mode.getCompanyState();
                if (state == 0) {
                    companys.add(mode);
                } else if (state == 1 || state == 2) {
                    AuthInfo info = mode.getAuthInfo();
                    if (info != null) {
                        String userName = info.getName();
                        if (!TextUtils.isEmpty(userName)) {
                            companys.add(mode);
                        }
                    }
                } else if (state == 3) {
                    companys.add(mode);
                }
            }
        }else{
            tabView.setVisibility(View.GONE);
            companys.add(company);
        }
        return companys;
    }

    @Override
    protected void setTabNames(List<OrganizaModel> companys) {
        List<String> tabTitles = new ArrayList<>();
        for(OrganizaModel organizaModel : companys)
            tabTitles.add(organizaModel.getCompanyFullName());
        tabView.setTabItems(tabTitles);
    }
    private TextView getView(){
        TextView view = new TextView(mContext);
        view.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        view.setGravity(Gravity.CENTER);
        return  view;
    }

    @Override
    public void finish() {
        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive() && this.getCurrentFocus() != null)
            if (this.getCurrentFocus().getWindowToken() != null)
                imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        super.finish();
    }
}
